﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Formatting;

namespace Utility
{
    public static class ErrorListManager
    {
        private static ErrorListProvider errorListProvider;
        public static ErrorListProvider GetErrorListProvider(SVsServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
                throw new ArgumentNullException();

            if (errorListProvider == null)
                errorListProvider = new ErrorListProvider(serviceProvider);
            return errorListProvider;
        }

        public static void AddErrorTask(SVsServiceProvider serviceProvider, ITextView textView, int position, ErrorCategory category, string fileName, string errorText)
        {
            if (string.IsNullOrWhiteSpace(fileName))
                throw new ArgumentNullException();
            if (string.IsNullOrWhiteSpace(errorText))
                throw new ArgumentNullException();

            TaskErrorCategory errorCategory = TaskErrorCategory.Message;
            switch (category)
            {
                case ErrorCategory.Error:
                    errorCategory = TaskErrorCategory.Error;
                    break;
                case ErrorCategory.Warning:
                    errorCategory = TaskErrorCategory.Warning;
                    break;
            }

            ErrorTask task = new ErrorTask();
            task.ErrorCategory = errorCategory;
            task.Document = fileName;
            task.Text = errorText;
            SnapshotPoint currentPoint = new SnapshotPoint(textView.TextSnapshot, position);
            ITextViewLine viewLine = textView.GetTextViewLineContainingBufferPosition(currentPoint);
            int line = currentPoint.GetContainingLine().LineNumber;
            int column = position - currentPoint.GetContainingLine().Start.Position;
            task.Line = line;
            task.Column = column;
            task.Navigate += (sender, e) =>
            {
                textView.Caret.MoveTo(currentPoint,PositionAffinity.Predecessor,true);
            };
            ErrorListProvider provider = GetErrorListProvider(serviceProvider);
            provider.Tasks.Add(task);
        }

        public static void RemoveErrorTask(SVsServiceProvider serviceProvider, string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
                throw new ArgumentNullException();

            ErrorListProvider provider = GetErrorListProvider(serviceProvider);
            List<ErrorTask> removeTargets = new List<ErrorTask>();
            foreach (ErrorTask task in provider.Tasks)
            {
                if (task.Document == fileName)
                    removeTargets.Add(task);
            }
            removeTargets.ForEach(t => provider.Tasks.Remove(t));
        }

        public static void ShowErrorList(SVsServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
                throw new ArgumentNullException();

            ErrorListProvider provider = GetErrorListProvider(serviceProvider);
            provider.Show();
        }
    }
}
